package com.drpclub.gamebackend.controller;

import com.drpclub.gamebackend.bean.ContentHtml;
import com.drpclub.gamebackend.bean.Resources;
import com.drpclub.gamebackend.common.constant.ActionUri;
import com.drpclub.gamebackend.common.constant.RootUri;
import com.drpclub.gamebackend.common.utils.DrpUtils;
import com.drpclub.gamebackend.common.utils.NetworkResult;
import com.drpclub.gamebackend.common.utils.log.RsHelper;
import com.drpclub.gamebackend.service.ContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.ch.Net;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.Callable;

import static com.drpclub.gamebackend.common.utils.DrpUtils.getToken;

/**
 * @author ：drp
 * @date ：Created in 2020/5/11 22:41
 * @description：主要内容
 */
@RequestMapping(RootUri.CONTENT)
@RestController
public class ContentController {

    private final ContentService contentService;

    @Autowired
    public ContentController(ContentService contentService) {
        this.contentService = contentService;
    }

    @ApiOperation("上传 文章 图片")
    @PostMapping(ActionUri.UPLOAD_CONTENT_IMAGE)
    public Callable<NetworkResult<Object>> upLoadContentImage(@RequestParam MultipartFile image,
                                                              @RequestParam String contentId,
                                                              HttpServletRequest request) {
        return () -> RsHelper.success (contentService.uploadContentImage (image, contentId, getToken (request)));
    }

    @ApiOperation("生成resource 资源")
    @PostMapping(ActionUri.CREATE_RESOURCE)
    public Callable<NetworkResult<Object>> createResource(@RequestBody @Valid Resources res,
                                                          HttpServletRequest request) {
        return () -> RsHelper.success (contentService.createResource (res, getToken (request)));
    }

    @ApiOperation("上传封面图片")
    @PostMapping(ActionUri.UPLOAD_COVER_IMAGE)
    public Callable<NetworkResult<Object>> upLoadCoverImage(@RequestParam MultipartFile image,
                                                            HttpServletRequest request) {
        return () -> RsHelper.success (contentService.uploadCoverImage (image, getToken (request)));
    }

    @ApiOperation("获取 resource 文集名")
    @GetMapping(ActionUri.GET_COLLECTION_NAMES)
    public Callable<NetworkResult<Object>> getCollectionNames(HttpServletRequest request) {
        return () -> RsHelper.success (contentService.getCollectionNames (getToken (request)));
    }

    @ApiOperation("根据 collectionName 获取 resource")
    @GetMapping(ActionUri.GET_RESOURCE)
    public Callable<NetworkResult<Object>> getResource(@RequestParam String title,
                                                       HttpServletRequest request) {
        return () -> RsHelper.success (contentService.getResource (title, getToken (request)));
    }

    @ApiOperation("上传文章 内容")
    @PostMapping(ActionUri.CREATE_CONTENT_HTML)
    public Callable<NetworkResult<Object>> createContent(@RequestBody ContentHtml content,
                                                         HttpServletRequest request) {
        return () -> RsHelper.success (contentService.createContentHtml (content, getToken (request)));
    }

    @ApiOperation("发布文章")
    @PutMapping(ActionUri.PUBLISH_CONTENT)
    public Callable<NetworkResult<Object>> publishContent(@RequestBody ContentHtml content,
                                                          HttpServletRequest request) {
        return () -> RsHelper.success (contentService.publishContentHtml (content, getToken (request)));
    }

    @ApiOperation("修改文章标题")
    @PutMapping(ActionUri.CHANGE_CONTENT_TITLE)
    public Callable<NetworkResult<Object>> changeContentTitle(String contentId, String title,
                                                              HttpServletRequest request) {
        return () -> RsHelper.success(contentService.changeContentTitle(contentId, title, getToken(request)));
    }
}
